// pages/job/job.js
const app = getApp();
var page = 1;
var index = 1;
var city = 0;
var word = false;//区分搜索分页
Page({
  /**
   * 页面的初始数据
   */
  data: {
    areaCheck: '全部',
    pay: '结算方式',
    select: '分类方式',
    order:'排序',
    recommended : [],
    secshow:false,
    wap: false,
    // screen: ['不限'],
    cover1isshow: false,
    code: '',
    navindex: 0,
    searchWords: '',
    nav: [
      { txt: '免费兼职' },
      { txt: '500强就业' },
      { txt: '人才推荐' }
    ],
    allservices: [[],[],[],],
    types: [
      { type: '', type2: '' },
      { type: '', type2: '' },
      { type: '' }
    ],
    hidden: ''    //页面样式
  },
  diqu:function(){
    wx.navigateTo({
      url: '../diqu/diqu',
    })
  },
  // 关闭
  open: function () {
    var types = !this.data.wap
    this.setData({
      wap: types,
      hidden:''
    })
  },
  //跳转详情
  start_recommended: function (e) {
    let that = this;
    // let cancel = e.currentTarget.dataset.cal;
    let cancel;
    let id = e.currentTarget.id;
    wx.request({
      url: app.data.Url+'job/isCancel',
      data: {'id':id},
      method: "post",
      success: function (res) {
        console.log(res);
        cancel = res.data;    
        console.log(cancel, 'cancel');
        if (cancel == 0) {
          wx.navigateTo({
            url: '../recommendedil/recommendedil?jobdeil=' + id,
          })
        } else {
          wx.showToast({
            title: '已结束',
            duration: 1000,
            mask: true
          })
        }
      }
    })

  },
  toshowc1: function (e) {
    var cover1isshow = !this.data.cover1isshow;
    this.setData({
      cover1isshow: cover1isshow,
      cover2isshow: false,
      cover3isshow: false,
      cover4isshow: false,    
    })
  },
  toshowc2: function (e) {
    var cover2isshow = !this.data.cover2isshow;
    this.setData({
      cover2isshow: cover2isshow,
      cover1isshow: false,
      cover3isshow: false,
      cover4isshow: false,
    })
  },
  toshowc3: function(e) {
    var cover3isshow = !this.data.cover3isshow;
    this.setData({
      cover3isshow: cover3isshow,
      cover1isshow: false,
      cover2isshow: false,
      cover4isshow: false,
    })
  },
  toshowc4: function (e) {
    var cover4isshow = !this.data.cover4isshow;
    this.setData({
      cover4isshow: cover4isshow,
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,
    })
  },
  click: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var txt = e.currentTarget.dataset.txt;
    var types = that.data.types
    types[index].type = txt
    this.setData({
      navindex: id,
      types: types
    })
  },
  waimask: function (e) {
    var index = e.currentTarget.id;
    var coverisshow = this.data.coverisshow;
    for (var i = 0; i < coverisshow.length; i++) {
      coverisshow[i] = false;
    }
    this.setData({
      coverisshow: coverisshow
    })
  },
  facus: function(){
    this.setData({
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,
      cover4isshow: false,
      secshow: false,
    }); 
  },
  tochangehp: function () {    
    this.setData({
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,      
      cover4isshow: false,      
      secshow: false,
      searchWords: ''
    }); 
    page = 1;
    word = false;
    this.setData({
      recommended: []
    })
    this.getList();
  },
  //筛选
  checkArea: function(e){
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      areaCheck : txt
    })
  },
  checkType: function(e){
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      select: txt
    })
  },
  checkSort: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      order: txt
    })
  },
  checkPay: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      pay: txt
    })
  },
// 筛选完
// 关键词搜索
  search: function(e){
    let searchWords = this.data.searchWords;
    let that = this;
    let p = 1;
    wx.setStorageSync(searchWords,searchWords);
    that.setData({
      areaCheck: '全部',
      pay: '结算方式',
      select: '分类方式',
      order: '排序',
      recommended:[]
    });
    that.getKeyList(searchWords);
  },
  searchWords: function(e){
    this.setData({
      searchWords: e.detail.value
    })
  },
  // 页面显示
  onShow: function(){
    let cityChange = wx.getStorageSync('cityChange');
    if(cityChange){
      this.upDate();
      wx.setStorageSync('cityChange', false);
    }
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // app.firstlogin();
    var openid = wx.getStorageSync('openid');
    var that = this;
    wx.request({
      url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/checkopenid',
      data: {
        openid: openid
      },
      method: "post",
      success: function (res) {
        wx.setStorageSync("uid", res.data); 
        if (res.data == 0) {
          that.setData({
            wap: true,
            hidden: 'hidden'
          })
        }
      }
    })
    wx.request({
      //获取openid接口  
      url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/getvip',
      data: {
        uid: wx.getStorageSync('uid')
      },
      method: 'post',
      success: function (resid) {
        wx.setStorageSync('step', resid.data);
      }
    })
    page = 1;
    that.setData({
      recommended: []
    })
    this.upDate();
  },
/**
* 从服务器获取列表
*/
  getList: function () {
    let that   = this;
    let city   = that.data.city;
    let area   = that.data.areaCheck;
    let pay    = that.data.pay;
    let order  = that.data.order;
    let select = that.data.select; 
    wx.request({
      url: app.data.Url + 'screen/search_list4',
      data: {
        screen: index,
        city  : city,
        area  : area,
        pay   : pay,
        order : order,
        page  : page,
        select: select
      },
      method: 'get',
      success: function (res) {
        if(res.data.data.length == 0){
          wx.showToast({
            title: '已加载全部',
            icon: 'success',
            duration: 3000
          })
        }
        let recommended = that.data.recommended;
        for (var i = 0; i < res.data.data.length; i++) {
          recommended.push(res.data.data[i]);
        }
        page++;
        that.setData({
          recommended: recommended
        })
      }
    })
  },
  // 搜索关键字
  getKeyList(searchWords){
    let that = this;
    word = true;
    wx.request({
      url: app.data.Url + 'screen/searchWords',
      data: {
        page: page,
        t   : 1,
        search: searchWords
      },
      success: function (res) {
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '已加载全部',
            icon: 'success',
            duration: 3000
          })
        }
        let recommended = that.data.recommended;
        for (var i = 0; i < res.data.data.length; i++) {
          recommended.push(res.data.data[i]);
        }
        page++;
        that.setData({
          recommended: recommended
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    var uid = wx.getStorageSync("uid"); 
    if (uid != '') {
    } else {
      wx.request({
        url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/userinfo',
        data: {
          uid: uid
        },
        method: "post",
        success: function (res) {
          wx.setStorageSync("usid", res.data[0].uid);
        }
      })
    }    
    // 获取用户基本信息
    wx.login({
      success: function (resss) {
        that.setData({
          code: resss.code
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  upDate: function () { 
    let that = this;
    that.setData({
      areaCheck: '全部',
      pay: '结算方式',
      select: '分类方式',
      order: '排序',
      recommended: []
    });
    page = 1;
    // 获取当前城市
    let city = wx.getStorageSync('city');
    this.setData({
      city:city
    });
    // 获取当前城市下区域列表
    wx.request({
      url: app.data.Url+'job/areas',
      data:{'city':city},
      success: function(res){
        that.setData({
          area:res.data[0],
          allservices:res.data[1],
          recommended: []
        })
        that.getList();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh:function(){
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    wx.showLoading({
      title: '拼命加载中……',
      duration: 1000
    })
    if (!word) {
      that.getList();
    }else{
      let searchWords = wx.getStorageInfoSync('searchWords');
      that.getKeyList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  sendRequest: function (e) {
    var that = this;
    var res = e.detail;
    //获取用户缓存token 此token是服务器作为用户唯一验证的标识，具体请看后端代码
    // var utoken = wx.getStorageSync("utoken");
    wx.setStorageSync("nickname", res.userInfo.nickName)
    wx.request({
      //用户登陆URL地址，请根据自已项目修改
      url: app.data.Url + 'User/logins',
      method: "POST",
      data: {
        // utoken: utoken,
        code: that.data.code,
        encryptedData: res.encryptedData,
        iv: res.iv,
        bid: 51
      },
      fail: function (res) {
        console.log('fail')
      },
      success: function (resa) {
        if (resa.data.success == 10) {
          console.log(resa.data.openid)
          wx.setStorageSync("openid", resa.data.openid);
          wx.request({
            //用户登陆URL地址，请根据自已项目修改
            url: app.data.Url + 'User/getUid',
            method: "POST",
            data: {
              openid: resa.data.openid
            },
            fail: function (res) {
            },
            success: function (res) {
              wx.setStorageSync("uid", res.data.data)
              // wx.setStorageSync("phone", res.data.phone)
              wx.setStorageSync("head", res.data.head)
              //    console.log(wx.getStorageSync("phone"))
              // 获取详细信息
              wx.request({
                url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/userinfo',
                data: {
                  uid: res.data.data
                },
                method: "post",
                success: function (res) {
                  wx.setStorageSync("usid", res.data[0].uid);
                }
              })
              that.setData({
                wap: false,
                hidden:''
              })
            }
          })
        }
        //设置用户缓存
        // wx.setStorageSync("utoken", utoken);
      }
    })
  },
})